今天要繼續說明該Camera 的使用方式,今天要介紹的是 Camera 的移動、拉伸與回到鏡頭的原始位置,今天牽涉到的數學也會比較多一點,但都很容易理解,那就讓我們開始介紹!
void Start()
{
transform.rotation = Quaternion.identity;
targetPosition = chickenModel.position;
}
if(Input.GetMouseButton(0))
{
dx *= moveSpeed;
dy *= moveSpeed;
targetPosition -= transform.up * dy + transform.right * dx;
}
請注意到 transform.up 是基於世界坐標系,Vector.up 是基於本地坐標系,transform 的特點就是增加物件旋轉時角度旋轉信息,所以說移動加上旋轉可同時產生。但是Vector 是僅針對單一角度的變化做處理。所以這邊採用如下計算:
targetPosition -= transform.up * dy + transform.right * dx;
void FixedUpdate() {
rotation = Quaternion.Slerp(rotation, targetRotation, Time.deltaTime * rotateLerp); // use Slerp to calculate
position = Vector3.Lerp(position, targetPosition, Time.deltaTime * moveLerp);
transform.rotation = rotation;
transform.position = position - rotation * new Vector3(0, 0, offsetZ);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl1 : MonoBehaviour
{
public Transform chickenModel;
float offsetZ = 2f, offsetY = 1f;
public Vector3 targetPosition, position; // store the position
private Quaternion rotation, targetRotation; // store the rotation
public float rotateSpeed = 32f, rotateLerp = 8f;
public float moveSpeed = 0.5f, moveLerp = 5f;
void Start()
{
transform.rotation = Quaternion.identity;
targetPosition = chickenModel.position;
}
void Update()
{
float dx = Input.GetAxis("Mouse X");
float dy = Input.GetAxis("Mouse Y");
if(Input.GetMouseButton(0))
{
dx *= moveSpeed;
dy *= moveSpeed;
targetPosition -= transform.up * dy + transform.right * dx;
}
if(Input.GetMouseButton(1))
{
dx *= rotateSpeed;
dy *= rotateSpeed;
if(Mathf.Abs(dx) > 0 || Mathf.Abs(dy) > 0)
{
// get the camera eular angle
Vector3 currentCameraAngle = transform.rotation.eulerAngles;
currentCameraAngle.x = Mathf.Repeat(currentCameraAngle.x + 180f, 360f) -180f;
currentCameraAngle.y += dx;
currentCameraAngle.x -= dy;
// store camera rotation angle
targetRotation.eulerAngles = new Vector3(currentCameraAngle.x , currentCameraAngle.y , 0);
}
}
}
void FixedUpdate() {
rotation = Quaternion.Slerp(rotation, targetRotation, Time.deltaTime * rotateLerp); // use Slerp to calculate
position = Vector3.Lerp(position, targetPosition, Time.deltaTime * moveLerp);
transform.rotation = rotation;
transform.position = position - rotation * new Vector3(0, 0, offsetZ);
}
}
void Start()
{
transform.rotation = Quaternion.identity;
targetPosition = chickenModel.position;
targetdistance = offsetZ; // set the first distance of the object
}
public float zoomSpeed = 8f, zoomLerp = 4f;
// control the scroll in or out
targetdistance -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
void FixedUpdate() {
// use Slerp to calculate
rotation = Quaternion.Slerp(rotation, targetRotation, Time.deltaTime * rotateLerp);
position = Vector3.Lerp(position, targetPosition, Time.deltaTime * moveLerp);
distance = Mathf.Lerp(distance, targetdistance, Time.deltaTime * zoomLerp);
transform.rotation = rotation;
transform.position = position - rotation * new Vector3(0, 0, distance);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl1 : MonoBehaviour
{
public Transform chickenModel;
float offsetZ = 2f, offsetY = 1f;
public Vector3 targetPosition, position; // store the position
private Quaternion rotation, targetRotation; // store the rotation
public float distance, targetdistance;
public float rotateSpeed = 32f, rotateLerp = 8f;
public float moveSpeed = 0.5f, moveLerp = 5f;
public float zoomSpeed = 8f, zoomLerp = 4f;
void Start()
{
transform.rotation = Quaternion.identity;
targetPosition = chickenModel.position;
targetdistance = offsetZ;
}
void Update()
{
float dx = Input.GetAxis("Mouse X");
float dy = Input.GetAxis("Mouse Y");
if(Input.GetMouseButton(0))
{
dx *= moveSpeed;
dy *= moveSpeed;
targetPosition -= transform.up * dy + transform.right * dx;
}
if(Input.GetMouseButton(1))
{
dx *= rotateSpeed;
dy *= rotateSpeed;
if(Mathf.Abs(dx) > 0 || Mathf.Abs(dy) > 0)
{
// get the camera eular angle
Vector3 currentCameraAngle = transform.rotation.eulerAngles;
currentCameraAngle.x = Mathf.Repeat(currentCameraAngle.x + 180f, 360f) -180f;
currentCameraAngle.y += dx;
currentCameraAngle.x -= dy;
// store camera rotation angle
targetRotation.eulerAngles = new Vector3(currentCameraAngle.x , currentCameraAngle.y , 0);
}
}
// control the scroll in or out
targetdistance -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
}
void FixedUpdate() {
rotation = Quaternion.Slerp(rotation, targetRotation, Time.deltaTime * rotateLerp); // use Slerp to calculate
position = Vector3.Lerp(position, targetPosition, Time.deltaTime * moveLerp);
distance = Mathf.Lerp(distance, targetdistance, Time.deltaTime * zoomLerp);
transform.rotation = rotation;
transform.position = position - rotation * new Vector3(0, 0, distance);
}
}